﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Net;
using System.Xml;
using System.ComponentModel;
using System.Reflection;

namespace PopShops
{
    public class CommonHelper
    {
        public static object Deserailizer(XmlReader stream, Type type)
        {
            object obj = new object();
            XmlSerializer serializer = new XmlSerializer(type);
            obj = serializer.Deserialize(stream);
            return obj;
        }

        public static object Deserailizer(TextReader stream, Type type )
        {
            object obj = new object();
            XmlSerializer serializer = new XmlSerializer(type);
            obj = serializer.Deserialize(stream);
            return obj;
        }

        public static object Deserailizer(string filepath, Type type)
        {
            object obj = new object();
            XmlSerializer serializer = new XmlSerializer(type);

            TextReader r = new StreamReader(filepath);
            obj = serializer.Deserialize(r);
            r.Close();

            return obj;
        }

        public static Stream WebClienta(string url)
        {
            WebClient web = new WebClient();
            return web.OpenRead(url);
        }

        public static string ConvertEnum(Enum obj)
        {
            FieldInfo info = obj.GetType().GetField(obj.ToString());
            DescriptionAttribute[] attribute = (DescriptionAttribute[])info.GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (attribute.Length > 0)
                return attribute[0].Description;

            return obj.ToString();
        }
    }

    public class Constants
    {
        public const string DateFormat = "mm/dd/yyyy";
    }
}
